package com.scmt.hmzd.controller;

import java.util.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.scmt.hmzd.entity.HmComm;
import com.scmt.hmzd.entity.HmOrderImg;
import com.scmt.hmzd.service.*;

import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.beans.factory.annotation.Autowired;
import com.scmt.core.common.utils.ResultUtil;
import com.scmt.core.common.vo.PageVo;
import com.scmt.core.common.vo.Result;
import com.scmt.core.common.vo.SearchVo;
import com.scmt.core.common.utils.SecurityUtil;
import com.scmt.hmzd.entity.HmOrder;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.scmt.core.common.enums.LogType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * @author
 **/
@RestController
@Api(tags = " 订单信息数据接口")
@RequestMapping("/scmt/hmOrder")
public class HmOrderController {
    @Autowired
    private IHmOrderService hmOrderService;

    @Autowired
    private IHmTechnicianService iHmTechnicianService;

    @Autowired
    private IHmCommService iHmCommService;
    @Autowired
    private IHmUserAddrService iHmUserAddrService;
    @Autowired
    private IHmProjectService iHmProjectService;
    @Autowired
    private IHmOrderImgService iHmOrderImgService;

    /**
     * 功能描述：新增订单信息数据
     *
     * @param hmOrder 实体
     * @return 返回新增结果
     */
    @ApiOperation("新增订单信息数据")
    @PostMapping("addHmOrder")
    public Result<Object> addHmOrder(@RequestBody HmOrder hmOrder) {
        try {
            hmOrder.setDelFlag(0);
            hmOrder.setCreateTime(new Date());
            boolean res = hmOrderService.save(hmOrder);
            if (res) {
                return ResultUtil.data(res, "保存成功");
            } else {
                return ResultUtil.data(res, "保存失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error("保存异常:" + e.getMessage());
        }
    }

    /**
     * 功能描述：更新数据
     *
     * @param hmOrder 实体
     * @return 返回更新结果
     */
    @ApiOperation("更新订单信息数据")
    @PostMapping("updateHmOrder")
    public Result<Object> updateHmOrder(@RequestBody HmOrder hmOrder) {
        if (hmOrder.getId() != null) {
            return ResultUtil.error("参数为空，请联系管理员！！");
        }
        try {
            boolean res = hmOrderService.updateById(hmOrder);
            if (res) {
                return ResultUtil.data(res, "修改成功");
            } else {
                return ResultUtil.data(res, "修改失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error("保存异常:" + e.getMessage());
        }
    }

    /**
     * 功能描述：根据主键来删除数据
     *
     * @param ids 主键集合
     * @return 返回删除结果
     */
    @ApiOperation("根据主键来删除订单信息数据")
    @PostMapping("deleteHmOrder")
    public Result<Object> deleteHmOrder(@RequestParam String[] ids) {
        if (ids == null || ids.length == 0) {
            return ResultUtil.error("参数为空，请联系管理员！！");
        }
        try {
            boolean res = hmOrderService.removeByIds(Arrays.asList(ids));
            if (res) {
                return ResultUtil.data(res, "删除成功");
            } else {
                return ResultUtil.data(res, "删除失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error("删除异常:" + e.getMessage());
        }
    }

    /**
     * 功能描述：根据主键来获取数据
     *
     * @param id 主键
     * @return 返回获取结果
     */
    @ApiOperation("根据主键来获取订单信息数据")
    @GetMapping("getHmOrder")
    public Result<Object> getHmOrder(@RequestParam(name = "id") String id) {
        if (StringUtils.isBlank(id)) {
            return ResultUtil.error("参数为空，请联系管理员！！");
        }
        try {
            HmOrder res = hmOrderService.getById(id);
            if (res != null) {
                res.setTechnician(iHmTechnicianService.getById(res.getTechnicianId()));
                QueryWrapper<HmComm> hmCommQueryWrapper = new QueryWrapper<>();
                hmCommQueryWrapper.eq("order_id", res.getId());
                hmCommQueryWrapper.orderByDesc("create_time");
                hmCommQueryWrapper.last("LIMIT 1");
                res.setComm(iHmCommService.getOne(hmCommQueryWrapper));
                res.setUserAddr(iHmUserAddrService.getById(res.getAddrOrderId()));
                res.setProjectList(Arrays.asList(iHmProjectService.getById(res.getProjectId())));

                QueryWrapper<HmOrderImg> queryWrapper = new QueryWrapper<>();
                queryWrapper.lambda().eq(HmOrderImg::getOrderId, id);
                res.setHmOrderImgList(iHmOrderImgService.list(queryWrapper));
                return ResultUtil.data(res, "查询成功");
            } else {
                return ResultUtil.data(res, "查询失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error("查询异常:" + e.getMessage());
        }
    }

    /**
     * 功能描述：实现分页查询
     *
     * @param searchVo 需要模糊查询的信息
     * @param pageVo   分页参数
     * @return 返回获取结果
     */
    @ApiOperation("分页查询订单信息数据")
    @GetMapping("queryHmOrderList")
    public Result<Object> queryHmOrderList(HmOrder hmOrder, SearchVo searchVo, PageVo pageVo) {
        try {
            IPage<HmOrder> result = hmOrderService.queryHmOrderListByPage(hmOrder, searchVo, pageVo);
            return ResultUtil.data(result);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error("查询异常:" + e.getMessage());
        }
    }

    /**
     * 功能描述：导出数据
     *
     * @param response 请求参数
     * @param hmOrder  查询参数
     * @return
     */
    @ApiOperation("导出订单信息数据")
    @PostMapping("/download")
    public void download(HttpServletResponse response, HmOrder hmOrder) {
        try {
            hmOrderService.download(hmOrder, response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @ApiOperation("首页统计数据")
    @GetMapping("/statisticsOrderData")
    public Result<Object> statisticsOrderData() {
        Map<String, Object> map = hmOrderService.statisticsOrderData();
        return ResultUtil.data(map);
    }


    @ApiOperation("首页本周数据")
    @GetMapping("/queryWeekData")
    public Result<Object> queryWeekData() {
        List<Map<String, Object>> map = hmOrderService.queryWeekData();
        return ResultUtil.data(map);
    }
}
